package leetCode;

public class t35 {
    public static void main(String[] args) {
        int[]nums={1,3};
        Solution.searchInsert(nums,2);
    }
    static class Solution {
        public static int searchInsert(int[] nums, int target) {
            int left=0;
            int right=nums.length-1;
            int middle=0;
            while (left<=right){
                middle=(left+right)/2;
                if (nums[middle]==target){
                    return middle;
                }else if(nums[middle]>target){
                    right=middle-1;
                }else {
                    left=middle+1;
                }
            }
            if (target>nums[middle]){
                return middle+1;
            }else {
                if(middle-1<0){
                    return 0;
                }
                return middle;
            }
        }
    }
}
